package com.jzqf.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class RectView extends View {
    private int mWidth = 220;
    private int mHeight = 240;
    private int mCenterX = 400;
    private int mCenterY = 640;

    public RectView(Context context) {
        super(context);
    }

    public RectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxis(canvas);
    }

    /**
     * 绘制坐标轴
     */
    private void drawAxis(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(2);
        canvas.drawRect(mCenterX - mWidth / 2f, mCenterY - mHeight / 2f,
                mCenterX + mWidth / 2f, mCenterY + mHeight / 2f, paint);
    }


    public void setWidthAndHeight(int width, int height, int centerX, int centerY) {
        this.mWidth = width;
        this.mHeight = height;
        this.mCenterX = centerX;
        this.mCenterY = centerY;
        invalidate();
    }
}
